﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.RPP.EntidadesNegocio;

namespace MJM.RPP.AccesoDatos
{
    public static class PromocionAD
    {
        private static String CambiarFecha(string Fecha)
        {
            return Fecha.Substring(6, 2) + "/" + Fecha.Substring(4, 2) + "/" + Fecha.Substring(0, 4);
        }

        public static int Insertar(PromocionEN objPromocion)
        {
            int codigo = 1;
            SqlConnection conn=new SqlConnection(Cadena.CadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_MAES_DESCOM_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_chPRODES", objPromocion.Promocion));
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_chRESDES", objPromocion.Restriccion));
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_chFECINI", objPromocion.FechaInicio));
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_chFECFIN", objPromocion.FechaFin));
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_byIMGLOG", objPromocion.Logo));
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_chNOMLOG", objPromocion.NombreLogo));
                cmd.Parameters.Add(new SqlParameter("@vCOMCON_P_inID", objPromocion.IdComercio));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return codigo;
        }

        public static List<PromocionEN> Listar(PromocionEN objPromocionEN)
        {
            List<PromocionEN> lst = new List<PromocionEN>();
            SqlConnection conn = new SqlConnection(Cadena.CadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_MAES_DESCOM_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vCOMCON_P_inID",objPromocionEN.IdComercio));
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    var obj = new PromocionEN();
                    obj.IdPromocion = dr.GetInt32(0);
                    obj.IdComercio = dr.GetInt32(8);
                    obj.Promocion = dr.GetString(1);
                    obj.Restriccion = dr.GetString(2);
                    obj.FechaInicio = CambiarFecha(dr.GetString(3));
                    obj.FechaFin = CambiarFecha(dr.GetString(4));
                    obj.Logo = (byte[])dr[5];
                    obj.Eliminado = dr.GetInt32(7);
                    obj.NombreLogo = dr.GetString(6);
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }

        public static int Eliminar(int IdPromocion)
        {
            SqlConnection conn=new SqlConnection(Cadena.CadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_MAES_DESCOM_Eliminar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            int codigo = 1;
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vDESCOM_P_inID", IdPromocion));
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                codigo = 0;
            }
            return codigo;
        }
    }
}
